package com.yxym.homework09;

public class homework09 {
    public static void main(String[] args) {
        //测试
        Teacher teacher = new Teacher("王三",'男',30,5);
        Student student =new Student("小明",'男',20,"2019110840");
        student.printInfo();
        student.study();
        System.out.println("================");
        teacher.printInfo();
        teacher.teach();

        //定义多态数组，里面保存2个学生和2个老师，要求按年龄从高到低排序
        Person[] person = new Person[4];
        person[0] = new Student("张三",'男',20,"2019110840");
        person[1] = new Student("李四",'男',22,"2019110841");
        person[2] = new Teacher("赵六",'男',30,5);
        person[3] = new Teacher("王五",'男',25,3);

        //创建对象
        homework09 homework09 = new homework09();
        homework09.bubbleSort(person);

        //输出排序后的数组
        System.out.println("====排序后的数组====");
        for (int i = 0; i < person.length; i++) {
            System.out.println(person[i]);
        }

        //遍历数组，调用test方法
        System.out.println("====================");
        for (int i = 0; i < person.length; i++) {//遍历多态数组
            homework09.test(person[i]);
        }
    }

    //定义方法，形参为Person类型，功能：调用学生的study或教师的teach方法
    //分析使用向下转型和向上转型判断
    public void test(Person p){
        if(p instanceof Student){//p的运行类型如果是Student
            ((Student) p).study();
        }else if(p instanceof Teacher ){
            ((Teacher) p).teach();
        }else{
            System.out.println("");
        }
    }

    public void bubbleSort(Person[] person){
        Person temp  = null;//中间值
        for (int i = 0; i < person.length-1 ; i++) {
            for (int j = 0; j < person.length -1 - j; j++) {
                //1.冒泡排序找出年龄较大的放前面
                if(person[j].getAge() < person[j+1].getAge()){
                    temp = person[j];
                    person[j] = person[j+1];
                    person[j+1] = temp;
                }
            }
        }
    }
}
